<?php
namespace app\admin\controller;

use app\common\controller\Base;
use app\common\model\ProductCat as ProductCatModel;
use app\common\service\ProductCat as ProductCatService;
use app\common\service\Product as ProductService;
use app\common\tool\Tool;
use think\Loader;
use think\Request;
use app\common\model\Product as ProductModel;
use app\common\model\ProductData as ProductDataModel;
use app\common\model\ProductFuncat as ProductFuncatModel;


class Product extends Base{

    public function index(){
        $productService = new ProductService();
        $products = $productService -> paginate();

        $productCatService = new ProductCatService();
        $productCat = $productCatService ->all();
        $productCat = collection($productCat)->toArray();
        $tool = new Tool();
        $productCat = $tool->changeArrayKey($productCat);


        return view('',['products'=>$products,'productCat'=>$productCat]);
    }

    public function add(){

        $productCatService = new ProductCatService();
        $sysCat = $productCatService -> treeByCatWay('sys');
        $funCat = $productCatService -> treeByCatWay('fun');

        return view('',['sysCat'=>$sysCat,'funCat'=>$funCat]);
    }

    public function create(Request $request){

        if(!$request->isPost()){
            $this->error("非法途径访问");
        }

        $data = $request->only(['name','sys_cat','fun_cat','cover','desc']);

        $productService = new ProductService();
        $result = $productService ->insert($data);

        if($result['state']) {

            $insertId = $result['insertId'];

            $funList = $request->only('funList');
            if(!$funList) $funList['funList'] = [];
            $funList = $funList['funList'];
            $funList = array_unique($funList);
            $addData = [];
            foreach($funList as $val){
                array_push($addData,['productid'=>$insertId,'funcatid'=>$val]);
            }


            $productFunCatModel = new ProductFuncatModel();
            $productFunCatModel->saveAll($addData);


            $this->success("操作成功");
        }else{
            $this->error($result['message']);
        }

    }

    public function edit(Request $request){
        $id = $request ->param('id',0,'intval');
        if(!$id){
            $this->error("非法途径访问");
        }

        $product = ProductModel::find($id);

        $productCatService = new ProductCatService();
        $sysCat = $productCatService -> treeByCatWay('sys');

        $funCat = $productCatService -> treeByCatWay('fun');


        $productFuncat = ProductFuncatModel::where('productid',$id)->select();
        $cat = ProductCatModel::all();
        $cat = collection($cat)->toArray();
        $tool = new Tool();
        $cat = $tool->changeArrayKey($cat);



        return view('',['product'=>$product,'sysCat'=>$sysCat,'funCat'=>$funCat,'cats'=>$cat,'productFunCat'=>$productFuncat]);
    }


    public function update(Request $request){
        if(!$request->isPost()){
            $this->error("非法途径访问");
        }

        $data = $request->only(['name','sys_cat','cover','fun_cat','desc']);
        $id = $request->param('id');
        if(!$id){
            $this->error("非法途径访问");
        }

        $funList = $request->only('funList');
        if(!$funList) $funList['funList'] = [];
        $funList = $funList['funList'];
        $funList = array_unique($funList);
        $addData = [];
        foreach($funList as $val){
            array_push($addData,['productid'=>$id,'funcatid'=>$val]);
        }


        ProductFuncatModel::destroy(['productid'=>$id]);
        $productFunCatModel = new ProductFuncatModel();
        $productFunCatModel->saveAll($addData);


        $validate = Loader::validate('ProductValidate');
        $result = $validate-> scene('edit')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }


        $productModel = ProductModel::find($id);
        $productModel->save($data);
        $this->success("操作成功");
    }


    public function banner(Request $request){
        $id = $request -> param('id',0,'intval');
        $product = ProductModel::find($id);

        $banner = unserialize($product->banner);
        if(!$banner){
            $banner =[];
        }

        return view('',['product'=>$product,'banner'=>$banner]);
    }

    public function bannerUpdate(Request $request){
        if(!$request->isPost()){
            $this->error("非法途径访问");
        }


        $banner = $request->only(['desc','images','image_alt']);
        $id = $request->param('id',0,'intval');
        if(!$id){
            $this->error("非法渠道访问");
        }

        if(!$banner){
            $banner = ['desc'=>[],'images'=>[]];
        }

        $storeBanner = [];
        foreach($banner['images'] as $key => $val ){
            $storeBanner[$key] = [];
            $storeBanner[$key]['image'] = $val;
            $storeBanner[$key]['desc'] = $banner['desc'][$key];
            $storeBanner[$key]['image_alt'] =  $banner['image_alt'][$key];
        }


        $storeBanner = serialize($storeBanner);
        $product = ProductModel::find($id);
        $product -> banner = $storeBanner;
        $product->save();

        $this->success("操作成功");
    }


    public function recommend(){

        $recommendProduct = ProductModel::where('is_recommend',1)->select();


        return view('',['recommendProduct'=>$recommendProduct]);
    }

    public function forRecommend(){

        $productList = ProductModel::order('id desc')->paginate(5);

        return view('',['productList'=>$productList]);
    }

    public function getProduct(Request $request){
        $id = $request->param('id',0,'intval');

        $product = ProductModel::find($id);
        if(!$product){
            $this->error("非法渠道访问");
        }
        $product->is_recommend = 1;
        $product->save();

        $product = $product->toArray();

        return json($product);
    }

    public function cancelRecommend(Request $request){
        $id = $request->param('id',0,'intval');
        $product = ProductModel::find($id);
        if(!$product){
            $this->error("非法渠道访问");
        }
        $product -> is_recommend = 0;
        $product->save();

        $this->success("操作成功");
    }



    public function assoc(Request $request){
        $id = $request->param('id',0,'intval');
        $product = ProductModel::find($id);
        if(!$product){
            $this->error("非法渠道访问");
        }

        $assoc = json_decode($product->assoc);
        if(!$assoc){
            $assoc = [];
        }

        $assocProducts = ProductModel::whereIn('id',$assoc)->select();


        return view('',['product'=>$product,'assocProducts'=>$assocProducts]);
    }

    public function forAssoc(Request $request){

        $productList = ProductModel::order('id desc')->paginate(5);

        return view('',['productList'=>$productList]);
    }

    public function addAssoc(Request $request){
        $id = $request->param('id',0,'intval');
        $assocId = $request->param('assocId',0,'intval');

        $product = ProductModel::find($id);
        if(!$product){
            $this->error("来源渠道非法");
        }
        $assoc  = json_decode($product->assoc);

        $assocProduct = ProductModel::find($assocId);

        if(!$assoc){
            $assoc = [$assocId];
        }else{
            if(in_array($assocId,$assoc)){
                $this->success("操作成功",null,json_decode($assocProduct));
            }else{
                array_push($assoc,$assocId);
            }
        }

        $product->assoc = json_encode($assoc);
        $product->save();

        $this->success("操作成功",null,json_decode($assocProduct));
    }

    public function cancelAssoc(Request $request){
        $id = $request->param('id',0,'intval');
        $assocId = $request->param('assocId',0,'intval');

        $product = ProductModel::find($id);
        if(!$product){
            $this->error("来源渠道非法");
        }
        $assoc  = json_decode($product->assoc);
        if(!$assoc){
            $this->success("操作成功");
        }else{
            $newAssoc = [];
            foreach($assoc as $val ){
                if($val != $assocId){
                    array_push($newAssoc,$val);
                }
            }
        }
        $product->assoc = json_encode($newAssoc);
        $product->save();


        $this->success("操作成功");
    }


    public function seo(Request $request){
        $id = $request->param('id',0,'intval');
        $product = ProductModel::find($id);
        if(!$product){
            $this->error("来源渠道非法");
        }

        return view('',['product'=>$product]);
    }

    public function seoUpdate(Request $request){
        if(!$request->isPost()){
            $this->error("非法途径访问");
        }

        $data = $request->only(['seo_title','seo_keywords','seo_description']);
        $id = $request->param('id',0,'intval');
        $productModel = ProductModel::find($id);
        if(!$productModel){
            $this->error("非法途径访问");
        }

        $productModel->save($data);
        $this->success("操作成功");
    }

    public function delete(Request $request){
        if(!$request->isDelete()){
            $this->error("非法访问途径");
        }

        $id = $request->param('id',0,'intval');

        ProductFuncatModel::where("productid",$id)->delete();
        ProductDataModel::where("productid",$id)->delete();
        $res = ProductModel::destroy($id);

        if($res){
            $this->success("操作成功");
        }else{
            $this->error("操作失败");
        }
    }

}
